{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7. Reverse Integer 反转整数\n",
    "\n",
    "### 难度：Easy\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/reverse-integer/description/\n",
    " - 英文：https://leetcode.com/problems/Reverse-Integer\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "给定一个 32 位有符号整数，将整数中的数字进行反转。\n",
    "\n",
    "示例 1:\n",
    "输入: 123\n",
    "输出: 321\n",
    "\n",
    "示例 2:\n",
    "输入: -123\n",
    "输出: -321\n",
    "\n",
    "示例 3:\n",
    "输入: 120\n",
    "输出: 21\n",
    "\n",
    "注意:\n",
    "假设我们的环境只能存储 32 位有符号整数，其数值范围是 [−231,  231 − 1]。根据这个假设，如果反转后的整数溢出，则返回 0。\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    " - x > 0, flag = 1； x < 0, flag = -1\n",
    " - 将 x 转成字符串 s = str(abs(x))，然后再反转字符串 s1 = s[::-1]\n",
    " - 字符串再转为整数：x1 = int(s1) * flag\n",
    " - 判断是否溢出，将数据返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n",
      "-654\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def reverse(self, x):\n",
    "        \"\"\"\n",
    "        :type x: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # 如果是负数，我们需要注意转化为绝对值\n",
    "        flag = 1\n",
    "        x_1 = 0\n",
    "        if x < 0:\n",
    "            flag = -1\n",
    "            x = int(str(abs(x))[::-1])\n",
    "            x_1 = x * flag\n",
    "        else:\n",
    "            flag = 1\n",
    "            x = int(str(x)[::-1])\n",
    "            x_1 = x * flag\n",
    "        if x_1 > 2**31-1 or x_1 < -2**31:\n",
    "            return 0\n",
    "        else:\n",
    "            return x_1\n",
    "\n",
    "s = Solution()\n",
    "x = 120\n",
    "y = -456\n",
    "print(s.reverse(x))\n",
    "print(s.reverse(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 2\n",
    "\n",
    " - 按照低位摘除，在按高位加上去其实就可以了\n",
    " - 利用循环，每次将最后一位取出来（求余），然后将原来的数据除以 10 ，取整，这样就可以每次都得到个位数\n",
    " - 按照取第二步，反向操作，取出来一个最后的个位数，然后乘以 10 ，再加上每一次得到的个位数，就实现了反转\n",
    " - 注意一个点，python 中对一个数求余时，结果与 求余 的符号相同，比如 5 % 2 = 1， 5 % (-2) = -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n",
      "-654\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def reverse(self, x):\n",
    "        \"\"\"\n",
    "        :type x: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        num = 0\n",
    "        flag = 1\n",
    "        if x > 0:\n",
    "            flag = 1\n",
    "        else:\n",
    "            flag = -1\n",
    "        while x != 0:\n",
    "            num = num * 10 + x % (10 * flag)\n",
    "            x = int(x / 10)\n",
    "        if num > 2**31-1 or num < -2**31:\n",
    "            return 0\n",
    "        else:\n",
    "            return num\n",
    "s = Solution()\n",
    "x = 120\n",
    "y = -456\n",
    "print(s.reverse(x))\n",
    "print(s.reverse(y))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
